const {
	getfiles,
	isDir,
	loadFile
} = require("./benTool")
const minify = require('html-minifier').minify;
const parseHtmlToAst = require("./benHtmlParst")
const csstree = require('css-tree');

let components = {}

const comsPath = "./src/components/"
getfiles(comsPath).forEach((f) => {
	let nextPath = comsPath + f.name
	if (isDir(nextPath)) {
		// c[f.name] = minify(loadFile(nextPath + "/" + f.name + ".vue"), {
		// 	caseSensitive: true,
		// 	collapseWhitespace: true
		// })

		let category = classification(loadFile(nextPath + "/" + f.name + ".vue"))
		let htmlAst = parseHtmlToAst(category.template)
		htmlAst.tag = "div"
		let cssAst = csstree.parse(category.style)
		components[f.name] = {
			template: htmlAst,
			style: cssAst
		}
	}
})

function classification(htmlStr) {

	let template = htmlStr.match(/<template>([\s\S]*?)<\/template>/)
	let style = htmlStr.match(/<style>([\s\S]*?)<\/style>/)
	let script = htmlStr.match(/<script>([\s\S]*?)<\/script>/)
	template = template ? `<template>${template[1]}</template>` : undefined
	style = style ? style[1] : undefined
	script = script ? script[1] : undefined
	return {
		template,
		style,
		script
	}
}

module.exports = components